package com.cloud.pole.rpc;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.cloud.pole.api.weixin.WxUserApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.weixin.model.entity.WxUser;
import com.cloud.pole.weixin.service.WxUserService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("api/wxUser")
public class WxUserRest {

    @Autowired
    private WxUserService wxUserService;
    
    @RequestMapping(value = "/getWxUser", method = RequestMethod.GET)
    public ApiRestResponse<WxUserApi> getWxUser(
  		  @RequestParam(name="openId", defaultValue = "") String openId){
		try {
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");
			WxUser wxUser = wxUserService.getWxUser(openId);
			WxUserApi wxUserApi = new WxUserApi();
			BeanUtils.copyProperties(wxUserApi, wxUser);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, wxUserApi);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
